package java44th.group.controller.admin.user;

import java44th.group.bean.common.BasePageInfo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketUser;
import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.service.admin.user.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ProjectName project-second
 * @Descriptiom TODO
 * @Aurhor: 蔡文强
 * @Since: 2022/10/12 10:44
 */
@RestController
@RequestMapping("admin/user")
public class UserController {

    @Autowired
    UserService userService;

    // 将Handler方法和权限建立耦合关系，访问这个Handler方法需要注解中的权限
    // 上了道锁
    // 注意 注解必须挨着写！！！
    @RequiresPermissions("*")
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public BaseRespVo user(BasePageInfo info,String username,String mobile){
        CommonData<MarketUser> data = userService.list(info,username,mobile);

        return BaseRespVo.ok(data);
    }

    // http://localhost:8083/admin/user/detail?id=1
    @RequiresPermissions("*")
    @RequestMapping(value = "detail",method =RequestMethod.GET )
    public BaseRespVo detail(String id){
        Integer integerId = null;
        try {
             integerId = Integer.parseInt(id);
        }catch (Exception e){
            return BaseRespVo.error(400,"请输入正确的用户id");
        }

        MarketUser marketUser = userService.queryUserById(integerId);

        return BaseRespVo.ok(marketUser);
    }

    //http://182.92.235.201:8083/admin/user/update
    @RequiresPermissions("*")
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public BaseRespVo update(@RequestBody MarketUser marketUser){

        userService.modifyUserUserObject(marketUser);

        return BaseRespVo.ok("1");
    }

}
